package net.exoweb.animate.actions
{
    import flash.display.*;

    import net.exoweb.animate.actions.Action;
    import net.exoweb.animate.common.Maths;
    import net.exoweb.animate.objects.Vector;

    public class UniformRotation extends Action
    {
        private var _angle:Number;
        private var _angleNow:Number;
        private var _axis:Vector;

        public function UniformRotation(angle:Number, speed:Number, axis:Vector=null)
        {
            super(speed);
            _angle = angle;
            _angleNow = 0;
            if(axis == null)
            {
                _axis = null;
            }
            else
            {
                _axis = new Vector(axis.x, axis.y);
            }
        }

        public function get angle():Number
        {
            return _angle;
        }

        public function set angle(v:Number):void
        {
            _angle = v;
            _angleNow = 0;
        }

        public function get axis():Vector
        {
            return _axis;
        }

        public function set axis(v:Vector):void
        {
            if(v == null)
            {
                _axis = null;
            }
            else
            {
                _axis.equal(v);
            }
        }

        override public function update(obj:DisplayObject, time:Number):void
        {
            var da:Number = time * _speed;
            var daf:Number = da % Maths.MAX_ANGLE;
            var dv:Vector = new Vector(0, 0);
            if(_axis != null)
            {
                dv.x = obj.x - _axis.x;
                dv.y = obj.y - _axis.y;
            }
            if(_angle > 0)
            {
                if(_angleNow + da <= _angle)
                {
                    obj.rotation = (obj.rotation + daf) % Maths.MAX_ANGLE;
                    if(_axis != null)
                    {
                        dv.equal(Maths.rotate(dv, daf));
                        obj.x = _axis.x + dv.x;
                        obj.y = _axis.y + dv.y;
                    }
                    _angleNow += da;
                }
                else
                {
                    obj.rotation = (obj.rotation + _angle - _angleNow) % Maths.MAX_ANGLE;
                    if(_axis != null)
                    {
                        dv.equal(Maths.rotate(dv, _angle - _angleNow));
                        obj.x = _axis.x + dv.x;
                        obj.y = _axis.y + dv.y;
                    }
                    _angleNow = _angle;
                    complete();
                }
            }
            else if(_angle < 0)
            {
                if(_angleNow - da >= _angle)
                {
                    obj.rotation = (obj.rotation + Maths.MAX_ANGLE - daf) % Maths.MAX_ANGLE;
                    if(_axis != null)
                    {
                        dv.equal(Maths.rotate(dv, Maths.MAX_ANGLE - daf));
                        obj.x = _axis.x + dv.x;
                        obj.y = _axis.y + dv.y;
                    }
                    _angleNow -= da;
                }
                else
                {
                    obj.rotation = (obj.rotation + Maths.MAX_ANGLE + _angle - _angleNow) % Maths.MAX_ANGLE;
                    if(_axis != null)
                    {
                        dv.equal(Maths.rotate(dv, Maths.MAX_ANGLE + _angle - _angleNow));
                        obj.x = _axis.x + dv.x;
                        obj.y = _axis.y + dv.y;
                    }
                    _angleNow = _angle;
                    complete();
                }
            }
        }
    }
}
